const app = getApp()
var videoAd = null;

Page({

  data: {
    userInfo: {},
    showThanks: false,
    admin: false,
    showAdmin: false,
    adminP: ''
  },
  onReady() {
    qq.showShareMenu({
      showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
    })
    this.setData({
      show: true
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo
      })
    } else {
      //  qq.redirectTo({
      //       url:"/pages/welcome/welcome"
      //   })
    }
    this.startMenu()
  },
  onHide() {
    console.log("hide")
    this.startMenu()
  },
  onShow() {
    if (this.data.show) {
      this.showMenu()
    } else {
      var that = this
      var s = setInterval(function () {
        if (that.data.show) {
          that.showMenu()
          clearInterval(s)
        }
      }, 10)
    }
  },
  startMenu() {
    const animation1 = qq.createAnimation({
      duration: 1,
      timingFunction: 'ease',
    })
    const animation2 = qq.createAnimation({
      duration: 1,
      timingFunction: 'ease',
    })

    animation1.translate(300, 0).step({ duration: 10 })
    this.setData({
      animationData1: animation1.export()
    })
    animation2.translate(-300, 0).step({ duration: 10 })
    this.setData({
      animationData2: animation2.export()
    })
  },
  showMenu() {
    const animation1 = qq.createAnimation({
      timingFunction: 'ease',
    })
    const animation2 = qq.createAnimation({
      timingFunction: 'ease',
    })

    animation1.translate(0, 0).step({ duration: 600 })
    this.setData({
      animationData1: animation1.export()
    })
    animation2.translate(0, 0).step({ duration: 600 })
    this.setData({
      animationData2: animation2.export()
    })
  },
  toMyPublish(e) {
    this.ableAddFriend()
    qq.navigateTo({
      url: '/pages/tabBar3/my_publish/my_publish?id=' + e.currentTarget.dataset.id
    })
  },
  ableAddFriend(e) {//已完成
    var that = this
    qq.getSetting({
      success(res) {
        //console.log(res)
        if (!res.authSetting['setting.addFriend']) {
          qq.authorize({
            scope: 'setting.addFriend',
            success() {
              app.globalData.ableAddFriend = true
            },
            fail() {
              setTimeout(function () {
                that.ableAddFriend();
              }, 7000)
              qq.showModal({
                title: "请求授权",
                content: "为了让其他人了解你,请授权【允许向我发起加好友】请求和【用户信息】",
                showCancel: false,
                confirmText: "去授权",
                success() {
                  qq.openSetting()
                }
              })
            }
          })
        }
      }
    })
  },
  saveAppToDesktop() {
    qq.saveAppToDesktop()
  },
  onShareAppMessage(res) {
    if (res.from === 'button') {
    }
    return {
      title: '河南大学校园QQ小程序',
      path: '/pages/tabBar1/tabBar1'
    }
  },
  showThanks() {
    this.setData({
      showThanks: !this.data.showThanks,
    })
  },
  toAboutUs() {
    qq.navigateTo({
      url: './about_us/about_us'
    })
  },
  toHandBook() {
    qq.navigateTo({
      url: "./hand_book/hand_book"
    })
  },
  ad1() {
    var that = this
    let appbox = qq.createAppBox({
      adUnitId: '019a458fc37758d896f2fa5cdaaa3c2b'
    })
    appbox.load().then(() => {
      appbox.show()
    })
    let videoAd = qq.createRewardedVideoAd({ adUnitId: 'c51f81768f71755785db8c6d290febc7' })
    videoAd.onError(function (res) { console.log('videoAd onError', res) })
    videoAd.onLoad(function (res) { console.log('videoAd onLoad', res) })
    videoAd.onClose(function (res) {
      console.log('videoAd onClose', res)
    })
    videoAd.load().then(() => {
      console.log('激励视频加载成功');
      videoAd.show().then(() => { console.log('激励视频 广告显示成功') }).catch(err => { console.log('激励视频 广告显示失败') })
    }).catch(err => { console.log('激励视频加载失败'); })
  },
  showAdmin() {
    if (this.data.admin) {
      app.globalData.admin = false
      this.setData({
        admin: false
      })
      qq.showToast({
        title: '已退出管理员模式'
      })
    } else {
      this.setData({
        showAdmin: true
      })
    }
  },
  hideAdmin() {
    this.setData({
      showAdmin: false
    })
  },
  inputAdmin(e) {
    this.setData({
      adminP: e.detail.value
    })
  },
  goAdmin() {
    if (this.data.adminP == 'Xjl123.') {
      qq.showToast({
        title: '已进入管理员模式'
      })
      app.globalData.admin = true
      this.setData({
        admin: true,
        showAdmin: false,
        adminP: ''
      })
    } else {
      qq.showToast({
        icon: 'none',
        title: '密码有误,请重试!'
      })
      this.setData({
        adminP: ''
      })
    }
  }


})
